package com.gdpu.project.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.components.FieldError;

import com.gdpu.page.vo.PageView;
import com.gdpu.page.vo.QueryResult;
import com.gdpu.project.service.TeacherService;
import com.gdpu.project.vo.Email;
import com.gdpu.project.vo.News;
import com.gdpu.project.vo.Teacher;
import com.opensymphony.xwork2.ActionSupport;

public class EamilAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private String receicer;
	private String sender;
	private String emailTitle;
	private String emailInfo;
	private String file;
	private int currentpage=1; 
	private PageView<Email> pageView=new PageView<Email>(10,currentpage);
	private TeacherService teacherService;

	private HttpServletRequest request;
	private HttpSession session;

	public String getReceicer() {
		return receicer;
	}

	public void setReceicer(String receicer) {
		this.receicer = receicer;
	}

	public String getSender() {
		return sender;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}

	public String getEmailTitle() {
		return emailTitle;
	}

	public void setEmailTitle(String emailTitle) {
		this.emailTitle = emailTitle;
	}

	public String getEmailInfo() {
		return emailInfo;
	}

	public void setEmailInfo(String emailInfo) {
		this.emailInfo = emailInfo;
	}

	public TeacherService getTeacherService() {
		return teacherService;
	}

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	public PageView<Email> getPageView() {
		return pageView;
	}

	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}

	/**
	 * 发送Email信息
	 * @return
	 */
	public String emailAddAction(){
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		Email email = new Email();
		email.setReceiver(getReceicer());
		email.setSender(getSender());
		email.setEmailTitle(getEmailTitle());
		email.setEmailInfo(getEmailInfo());
		email.setFsTime(new Date());
		email.setReadFlag(0);
		Boolean flag = teacherService.addEmail(email);
		
		int pageNo = 1;
		int pageCount = 0;
		try {
			pageNo = Integer.parseInt(request.getParameter("pageNo"));
		} catch (Exception ex) {
		}
		if (pageNo < 1) {
			pageNo = 1;
		}
		pageCount = teacherService.countOfEmail();
		
		if(flag){
			List<News> newslist = teacherService.splitPage(pageNo);
			request.setAttribute("pageCount", pageCount);
			request.setAttribute("pageNo", pageNo);
			request.setAttribute("newslist", newslist);
			return "suucess";
		}else{
			return "failed";
		}
	}
	
	/**
	 * 删除Email
	 * @return
	 */
	public String eamilDeleteAction(){
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		
		int id=Integer.parseInt(request.getParameter("id"));
		@SuppressWarnings("unused")
		Boolean flag = teacherService.deleteEmailById(id);
		if("rec".equals(request.getParameter("maillistType"))){
			listAllRecEmail();
			return "reclist";
		}
		listAllSendEmail();
		return "sendlist";
	}
	public String eamilCheck(){
		request = ServletActionContext.getRequest();
		int id=Integer.parseInt(request.getParameter("id"));
		Email email=teacherService.findEmailById(id);
		request.setAttribute("email", email);
		return SUCCESS;
	}
	public String listAllSendEmail(){
		request = ServletActionContext.getRequest();
		session = request.getSession();
		Teacher teacher=(Teacher)session.getAttribute("teacher");
		QueryResult<Email> queryResult = teacherService.findEmailBySender(teacher.getName(),pageView.getFirstResult(), pageView.getMaxresult());
		pageView.setQueryResult(queryResult);
		request.setAttribute("pageView", pageView);
		request.setAttribute("emaillist",queryResult.getResultlist());
		return "success";
	}
	public String listAllRecEmail(){
		request = ServletActionContext.getRequest();
		session = request.getSession();
		Teacher teacher=(Teacher)session.getAttribute("teacher");
		QueryResult<Email> queryResult = teacherService.findEmailByReceiver(teacher.getName(),pageView.getFirstResult(), pageView.getMaxresult());
		pageView.setQueryResult(queryResult);
		request.setAttribute("pageView", pageView);
		request.setAttribute("emaillist",queryResult.getResultlist());
		return "success";
	}

}
